<?php
/**
 * 【评价控制器】
 * Created by PhpStorm.
 * User: Administrater
 * Date: 2018-06-14
 * Time: 10:33
 */

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Http\model\Curdpost;
class FinishController extends Controller
{
    /**
     * [订单结束待评价接口]
     * @Author 孙凯
     * @DateTime 2018-06-14
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $file = $request->all();
        if (empty($file['num'])) {
            return response(['msg' => '', 'message' => '参数请填写完整', 'code' => '1011'], 400);
        }
        $num = $file['num'];
        try {
            $arr = (JWTAuth::parseToken()->authenticate());
            $arr = json_decode($arr, true);
            $id = $arr['id'];
            $select = [
                'original',//上车地
                'end',//下车地
                'originaldegree',//上车地经纬度
                'enddegree',//下车地经纬度
                'implementstatus',//'订单执行状态1下单，2只派司机，3司机接单，4备车，5前往预约地点，6到达指定地，	7开始服务，8即将到达，9结束服务',
            ];

            $handle = DB::table('order')
                ->join('thought', 'order.num', '=', 'thought.num');
            $datas = $handle->select($select)->where([
                ['uid', '=', $id],
                ['order.num', '=', $num],
                ['order.implementstatus', '=', 9],
            ])->get()->toarray();
            $msg = json_decode(json_encode($datas), true);

            return $msg ? response(['msg' => $msg, 'message' => '数据获取成功', 'code' => '1000'], 200) : response(['msg' => '', 'message' => '数据获取失败', 'code' => '1011'], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => '1011', 'message' => '非法的token']);
        }


    }

    /**
     * [订单结束评价完成接口展示]
     * @Author 孙凯
     * @DateTime 2018-06-14
     * @param Request $request
     * @return array
     */
    public function end(Request $request)
    {
        $file = $request->all();
        if (empty($file['num'])) {
            return response(['msg' => '', 'message' => '参数请填写完整', 'code' => '1011'], 400);
        }
        $num = $file['num'];
        try {
            $arr = (JWTAuth::parseToken()->authenticate());
            $arr = json_decode($arr, true);
            $id = $arr['id'];
            $select = [
                'original',//上车地
                'end',//下车地
                'originaldegree',//上车地经纬度
                'enddegree',//下车地经纬度
                'implementstatus',//'订单执行状态1下单，2只派司机，3司机接单，4备车，5前往预约地点，6到达指定地，	7开始服务，8即将到达，9结束服务',
            ];

            $handle = DB::table('order');
            $datas = $handle->select($select)
                ->where([
                    ['uid', '=', $id],
                    ['order.num', '=', $num],
                    ['order.implementstatus', '=', 9],
                ])
                ->get()->toarray();
            $msg = json_decode(json_encode($datas), true);

            return $msg ? response(['msg' => $msg, 'message' => '评价成功', 'code' => '1000'], 200) : response(['msg' => '', 'message' => '评价失败', 'code' => '1011'], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => '1011', 'message' => '非法的token']);
        }
    }

    /**
     * [已评价接口]
     * @Author 孙凯
     * @DateTime 2018-06-14
     * @param Request $request
     * @return array
     */
    public function over(Request $request)
    {
        $file = $request->all();

        if (empty($file['num'])) {
            return response(['msg' => '', 'message' => '参数请填写完整', 'code' => '1011'], 400);
        }
        $num = $file['num'];
        try {
            $arr = (JWTAuth::parseToken()->authenticate());
            $arr = json_decode($arr, true);
            $id = $arr['id'];
            $select = [
                'original',//上车地
                'end',//下车地
                'originaldegree',//上车地经纬度
                'implementstatus',//'订单执行状态1下单，2只派司机，3司机接单，4备车，5前往预约地点，6到达指定地，	7开始服务，8即将到达，9结束服务',
                'enddegree',//下车地经纬度
                'thought.server',//服务满意度:1:一星,2:2星,3:3星,4:4星,5:5星
                'thought.driver',//'驾驶员满意度:1:一星,2:2星,3:3星,4:4星,5:5星'
                'thought.car',//'车辆满意度:1:一星,2:2星,3:3星,4:4星,5:5星'
            ];

            $handle = DB::table('order')
                ->join('thought', 'order.num', '=', 'thought.num');
            $datas = $handle->select($select)->where([
                ['uid', '=', $id],
                ['order.num', '=', $num],
                ['order.implementstatus', '=', 9],
            ])
                ->get()->toarray();
            $msg = json_decode(json_encode($datas), true);

            return $msg ? response(['msg' => $msg, 'message' => '已评价数据获取成功', 'code' => '1000'], 200) : response(['msg' => '', 'message' => '已评价数据获取失败 ', 'code' => '1011'], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => '1011', 'message' => '非法的token']);
        }

    }

    /**
     * [评价接口]
     * @Author 孙凯
     * @DateTime 2018-06-14
     * @param Request $request
     * @return array
     */
    public function add(Request $request)
    {
        $file = $request->all();
        if (empty($file['num']) || empty($file['server']) || empty($file['driver']) || empty($file['car'])) {
            return response(['msg' => '', 'message' => '请进行全部评价', 'code' => '1011'], 400);
        }
        $num = $file['num'];
        try {
            JWTAuth::parseToken()->authenticate();
            //$arr = json_decode($arr, true);
            $select = [
                'server' => $file['server'],//服务满意度:1:一星,2:2星,3:3星,4:4星,5:5星
                'driver' => $file['driver'],//'驾驶员满意度:1:一星,2:2星,3:3星,4:4星,5:5星'
                'car' => $file['car'],//'车辆满意度:1:一星,2:2星,3:3星,4:4星,5:5星'
                'create_time' => time(),
            ];
            $handle = DB::table('thought');
            $datas = $handle
                ->where([
                    ['num', '=', $num],
                ])->update($select);
            $msg = json_decode(json_encode($datas), true);
            if($msg){
                $all = ['CompanyId'=>'test','ServiceScore'=>$file['server'],'OrderId'=>$num,'EvaluateTime'=>date("YmdHis",time())];
                $news = new Curdpost();
                $list = $news->NewInsert("ratedpassenger",$all);
               // return $list ? response(['msg' => $msg, 'message' => '同步成功', 'code' => '1000'], 200):'同步失败';
            }else{
              // return response(['msg' => '', 'message' => '同步失败', 'code' => '1011'], 400);
            }

            return $msg ? response(['msg' => $msg, 'message' => '评价操作成功', 'code' => '1000'], 200) : response(['msg' => '', 'message' => '评价操作失败', 'code' => '1011'], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => '1011', 'message' => '非法的token']);
        }

    }


    /**生成token
     * @return mixed
     */
    public function test()
    {
        $user = User::where('id', 1)->select('id')->first();
        return JWTAuth::fromUser($user);
    }
}